package com.oyjd.ybx.front.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestUtils;

import com.oyjd.ybx.bean.vo.ClassifyVO;
import com.oyjd.ybx.bean.vo.GoodsInfoVO;
import com.oyjd.ybx.bean.vo.InformationVO;
import com.oyjd.ybx.business.classify.service.ClassifyService;
import com.oyjd.ybx.business.goods.service.GoodsService;
import com.oyjd.ybx.business.goods.vo.GoodsInfoExVO;
import com.oyjd.ybx.business.msg.service.MsgService;
import com.oyjd.ybx.fw.action.BaseAction;
import com.oyjd.ybx.fw.action.Page;
import com.oyjd.ybx.fw.util.CodeUtil;
import com.oyjd.ybx.fw.util.OutUtil;
import com.oyjd.ybx.fw.util.RandUtil;
import com.oyjd.ybx.pub.Const;

/**
 * 
 * 前端Action控制层
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Action(value = "/front")
@Results({ @Result(name = "viewIndex", location = Const.JSP_front_index), @Result(name = "goDetail", location = Const.JSP_front_detail), @Result(name = "viewThird", location = Const.JSP_front_goThird), @Result(name = "viewSearch", location = Const.JSP_front_goSearch), @Result(name = "viewContextDetail", location = Const.JSP_front_goContextDetail), @Result(name = "viewContextList", location = Const.JSP_front_goContextList) })
public class FrontAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 12534347027015039L;

	private static final Log log = LogFactory.getLog(FrontAction.class);

	private GoodsService goodsService;

	private ClassifyService classifyService;

	private MsgService msgService;

	@Autowired
	public void setMsgService(MsgService msgService) {
		this.msgService = msgService;
	}

	@Autowired
	public void setClassifyService(ClassifyService classifyService) {
		this.classifyService = classifyService;
	}

	@Autowired
	public void setGoodsService(GoodsService goodsService) {
		this.goodsService = goodsService;
	}

	/**
	 * 进入首页主页面
	 * 
	 * @return
	 */
	public String viewIndex() {
		// 分类树
		getRequest().setAttribute("treeMap", classifyService.getFrontClass());
		// 资讯列表
		getRequest().setAttribute("msgMap", msgService.getFrontInfo());
		// 获取最新上线数量
		getRequest().setAttribute("newCountMap", goodsService.getNewGoodsCount());
		// 获取热门的第三分类
		getRequest().setAttribute("hotClassMap", goodsService.getHotClass3());
		return "viewIndex";
	}

	/**
	 * 获取商品信息列表数据
	 * 
	 * @return
	 */
	public String getList() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			Page<GoodsInfoExVO> page = goodsService.getFrontList(new Page<GoodsInfoExVO>(req));
			//
			map.put("count", page.getCount());
			map.put("info", page.getResult());
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 获取猜你喜欢商品信息列表数据
	 * 
	 * @return
	 */
	public String getGuessList() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			int num = ServletRequestUtils.getIntParameter(req, "num", 5);
			Page<GoodsInfoExVO> page = new Page<GoodsInfoExVO>(req);
			page.setPage(1);
			page.setPageSize(200);
			goodsService.getFrontList(page);
			//
			map.put("info", RandUtil.getRandList(page.getResult(), num));
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 进入明细
	 * 
	 * @return
	 */
	public String goDetail() {
		HttpServletRequest req = getRequest();
		try {
			String goodsId = ServletRequestUtils.getStringParameter(req, "goodsId");
			GoodsInfoExVO vo = goodsService.getGoodsInfoVO(goodsId);
			goodsService.countLook(goodsId, req.getRemoteAddr());
			req.setAttribute("data", vo);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
		}
		// 分类树
		getRequest().setAttribute("treeMap", classifyService.getFrontClass());
		return "goDetail";
	}

	/**
	 * 跳转链接
	 * 
	 * @return
	 */
	public String toLink() {
		try {
			HttpServletRequest req = getRequest();
			String id = ServletRequestUtils.getStringParameter(req, "id");
			GoodsInfoVO vo = goodsService.getGoodsInfoVO(id);
			getResponse().sendRedirect(vo.getLink());
		} catch (Exception err) {
			log.error(err.getMessage(), err);
		}
		return null;
	}

	/**
	 * 获取特定商品分类的随机资讯
	 * 
	 * @return
	 */
	public String getRandMsgByClassId() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			int num = ServletRequestUtils.getIntParameter(req, "num", 8);
			String classId = ServletRequestUtils.getStringParameter(req, "classId");
			Map<ClassifyVO, Map<ClassifyVO, List<ClassifyVO>>> tmp = classifyService.getFrontClass();
			String pClassId = "A000";
			out: for (ClassifyVO top : tmp.keySet()) {
				Map<ClassifyVO, List<ClassifyVO>> m2 = tmp.get(top);
				for (ClassifyVO level2 : m2.keySet()) {
					List<ClassifyVO> m3 = m2.get(level2);
					for (ClassifyVO level3 : m3) {
						if (level3.getClassId().equals(classId)) {
							pClassId = top.getClassId();
							break out;
						}
					}
				}
			}

			//
			map.put("info", msgService.getRandMsg(pClassId, num));
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 进入第三层类
	 * 
	 * @return
	 */
	public String viewThird() {
		try {
			HttpServletRequest req = getRequest();
			String classId = ServletRequestUtils.getStringParameter(req, "classId");
			Map<ClassifyVO, Map<ClassifyVO, List<ClassifyVO>>> tmp = classifyService.getFrontClass();
			String name1 = "", name2 = "", name3 = "";
			out: for (ClassifyVO top : tmp.keySet()) {
				Map<ClassifyVO, List<ClassifyVO>> m2 = tmp.get(top);
				for (ClassifyVO level2 : m2.keySet()) {
					List<ClassifyVO> m3 = m2.get(level2);
					for (ClassifyVO level3 : m3) {
						if (level3.getClassId().equals(classId)) {
							name1 = top.getClassName();
							name2 = level2.getClassName();
							name3 = level3.getClassName();
							break out;
						}
					}
				}
			}
			//
			req.setAttribute("classId", classId);
			req.setAttribute("name1", name1);
			req.setAttribute("name2", name2);
			req.setAttribute("name3", name3);
			// 分类树
			getRequest().setAttribute("treeMap", classifyService.getFrontClass());

		} catch (Exception err) {
			log.error(err.getMessage(), err);
		}
		return "viewThird";
	}

	/**
	 * 进入搜索
	 * 
	 * @return
	 */
	public String viewSearch() {
		try {
			HttpServletRequest req = getRequest();
			String searchKey = ServletRequestUtils.getStringParameter(req, "searchKey");
			req.setAttribute("searchKey", searchKey);
			// 分类树
			getRequest().setAttribute("treeMap", classifyService.getFrontClass());

		} catch (Exception err) {
			log.error(err.getMessage(), err);
		}
		return "viewSearch";
	}

	/**
	 * 进入文章更多列表
	 * 
	 * @return
	 */
	public String viewContextList() {
		HttpServletRequest req = getRequest();
		String classId = ServletRequestUtils.getStringParameter(req, "classId", "");
		req.setAttribute("classId", classId);
		// 分类树
		getRequest().setAttribute("treeMap", classifyService.getFrontClass());
		return "viewContextList";
	}

	/**
	 * 进入文章明细
	 * 
	 * @return
	 */
	public String viewContextDetail() {
		try {
			HttpServletRequest req = getRequest();
			String msgId = ServletRequestUtils.getStringParameter(req, "msgId");
			InformationVO vo = msgService.getInformationVO(msgId);
			String msg = vo.getMsgContent();
			msg = msg.replaceAll(" ", "&nbsp;&nbsp;");
			msg = msg.replaceAll("\\r\\n", "<br/>");
			vo.setMsgContent(msg);
			req.setAttribute("data", vo);
			// 相关列表
			List<InformationVO> list = msgService.getRandMsg(vo.getClassId(), 4);
			req.setAttribute("aboutData", list);
			// 计数器
			msgService.countLook(msgId, req.getRemoteAddr());
		} catch (Exception err) {
			log.error(err.getMessage(), err);
		}
		// 分类树
		getRequest().setAttribute("treeMap", classifyService.getFrontClass());
		return "viewContextDetail";
	}

	/**
	 * 获取资讯信息列表数据
	 * 
	 * @return
	 */
	public String getContextList() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			Page<InformationVO> page = msgService.getList(new Page<InformationVO>(req));
			//
			map.put("count", page.getCount());
			map.put("info", page.getResult());
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}
	
}
